﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Jb.Common.Extensions;
using Jb.Domain;
using Jb.Web.ViewModels;
using Jb.Web.Utility;
using AutoMapper;
using Jb.Web.ViewModels.UtilityModels;

namespace Jb.Web.Controllers
{
    public class RssFeedController : BaseController
    {
		private JbEntities _Context = new JbEntities();
        
		#region Constructors
		public RssFeedController()
		{
		}

		public RssFeedController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods
        
        public ActionResult Index(String from, String to, String pageNo, String pageSize)
        {
            int maxRecordPerPage = 50;
            JobFeedViewModel model = new JobFeedViewModel();
            
            model.FromDate = from.IsNullOrEmpty() ? DateTime.Now.SqlDateTimeMinValue() : from.ToDateTime();            
            model.ToDate = to.IsNullOrEmpty() ? DateTime.Now.SqlDateTimeMinValue() : to.ToDateTime();

            model.PageNo = pageNo.ToInt() > 0 ? pageNo.ToInt() : 1;
            model.PageSize = pageSize.ToInt();
            model.PageSize = model.PageSize > 0 && model.PageSize < maxRecordPerPage ? model.PageSize : maxRecordPerPage;
            model.TotalJobs = (from P in _Context.Jobs
                               where P.IsActive && P.Company.IsActive && !P.IsDeleted
                               && P.PostDate >= model.FromDate && P.PostDate <= model.ToDate
                               select P).Count();

            var entities = (from P in _Context.Jobs
                               .Include("Company")
                               .Include("JobLevel")
                                //.Include("JobEducations").Include("JobSkills")
                                .Include("City")
                           where P.IsActive && P.Company.IsActive && !P.IsDeleted
                           && P.PostDate >= model.FromDate && P.PostDate <= model.ToDate
                           select P)
                           .OrderByDescending( P => P.PostDate)
                           .Skip((model.PageNo - 1) * model.PageSize).Take(model.PageSize)
                           .ToList();
            
            model.FoundJobs = Mapper.Map<List<Job>, List<JobViewModel>>(entities);
            ///Set Job Location
            if (model.FoundJobs != null && model.FoundJobs.Count > 0)
            {
                for (int i = 0; i < entities.Count; i++)
                {
                    Job job = entities[i];
                    JobViewModel viewModel = model.FoundJobs[i];
                    JobUtil.SetJobLocation(job, viewModel, "/");
                }
            }
            return View(model);
        }
	
		#endregion Action Methods
		
		#region Helper Methods
		

        #endregion			
    }
}


